<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="addRules">增加停车计费规则</el-button>
      <el-button @click="exportExecl">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            <el-tag :type="formatInfo(scope.row.chargeType)">{{ formatType(scope.row.chargeType) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="hEdit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="hDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="page-container">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="query.pageSize"
          :current-page="query.page"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 使用子组件 -->
    <my-dialog ref="myDialog" :dialog-visible.sync="dialogVisible" />
  </div>
</template>

<script>
import { deleteRuleAPI, getRuleListAPI } from '@/api/rule'
import { utils, writeFileXLSX } from 'xlsx'
import myDialog from './components/myDialog.vue'
export default {
  name: 'CarRule',
  components: { myDialog },
  data() {
    return {
      ruleList: [],
      query: {
        page: 1, // 当前页码
        pageSize: 10 // 每一页显示多少条数据
      },
      total: 0,
      dialogVisible: false // 控制弹出框的显示与隐藏
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    // 编辑计费规则
    hEdit(id) {
      // 显示dialog
      this.dialogVisible = true
      // 调用子组件的方法
      this.$refs.myDialog.getRuleDetail(id)
      this.$refs.myDialog.title = '编辑计费规则'
    },
    // 删除计费规则
    hDel(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await deleteRuleAPI(id)
          this.$message.success('删除成功')
          this.getRuleList()
        } catch (error) {
          if (error.response) {
            this.$message.error(error.response.data.msg)
          }
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 显示弹出框
    addRules() {
      this.dialogVisible = true
      this.$refs.myDialog.title = '添加计费规则'
    },
    // 格式化计费方式
    formatType(type) {
      const obj = {
        duration: '时长收费',
        turn: '按次收费',
        partition: '分段收费'
      }
      return obj[type]
    },
    formatInfo(type) {
      const obj = {
        duration: 'success',
        turn: 'warning',
        partition: 'danger'
      }
      return obj[type]
    },
    exportExecl() {
      // 定义表头
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      // console.log(this.ruleList)
      const sheetArr = this.ruleList.map(item => {
        // 创建空对象
        const obj = {}
        // 遍历 tableHeaderKeys 这个数据
        tableHeaderKeys.forEach(key => {
          // 给obj对象添加属性和属性值
          // obj[key] = value
          obj[key] = item[key]
        })
        return obj
      })
      // console.log(sheetArr)
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表 要求一个对象数组格式
      const worksheet = utils.json_to_sheet(sheetArr)

      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'sheet1')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, 'bj.xlsx')
    },
    // 分页切换
    handleCurrentChange(val) {
      // console.log(val)
      this.query.page = val
      this.getRuleList()
    },
    // 获取计费规则
    async getRuleList() {
      try {
        const res = await getRuleListAPI(this.query)
        // console.log(res.data)
        this.ruleList = res.data.rows
        this.total = res.data.total
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
</style>
